﻿using System;
using System.Collections;
using Gp.Scripts.Core;
using Sirenix.OdinInspector;
using UnityEngine;

namespace Gp.AI {
    [CreateNodeMenu("开始节点")]
    [DisallowMultipleNodes]
    [NodeWidth(400)]
    public class StartNode : BasicNode {
        [Output(connectionType: ConnectionType.Override)] [LabelText("防御轮行为")] [SerializeField]
        public BaseNode dfsTurn;

        [Output(connectionType: ConnectionType.Override)] [LabelText("进攻轮行为")] [SerializeField]
        private BaseNode atkTurn;


        [LabelText("攻击权重")] public AttackTargetWeight attackWeight;


        public BaseNode AtkTurnNode => GetOutputPort("atkTurn").Connection?.node as BaseNode;
        public BaseNode DfsTurnNode => GetOutputPort("dfsTurn").Connection?.node as BaseNode;


        public override AINodeEntry NodeEntry() {
            return new StartAINodeEntry(this);
        }

# if UNITY_EDITOR
        private void Reset() {
            attackWeight.Reset();
        }
# endif
    }


    public class StartAINodeEntry : AINodeEntry {
        public TurnState State;

        public StartAINodeEntry(StartNode startNode) {
            StartNode = startNode;
        }

        public void Init(TurnState state, BaseUnit unit) {
            State = state;
            Unit = unit;
        }

        public override IEnumerator Execute() {
            if (State == TurnState.Attack) {
                yield return ToNext(StartNode.AtkTurnNode);
            }
            else if (State == TurnState.Defense) {
                yield return ToNext(StartNode.DfsTurnNode);
            }
        }
    }
}